package cc.magicjson.example.demo.chatgpt;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;

enum Door0 {
    FIRST, SECOND, THIRD
}

class MontyHallSimulation0 {
    private static final Random random = new Random();

    public static void main(String[] args) {
        int totalSimulations = 1000;
        long winCount = simulateMontyHall(totalSimulations);
        double winPercentage = (double) winCount / totalSimulations * 100;

        System.out.println("Simulation Results:");
        System.out.println("Total Simulations: " + totalSimulations);
        System.out.println("Wins: " + winCount);
        System.out.println("Win Percentage: " + winPercentage + "%");
    }

    private static long simulateMontyHall(int totalSimulations) {
        return java.util.stream.IntStream.range(0, totalSimulations)
                .filter(i -> simulateSingleMontyHallGame())
                .count();
    }

    private static boolean simulateSingleMontyHallGame() {
        List<Door0> door0s = new ArrayList<>();
        door0s.add(Door0.FIRST);
        door0s.add(Door0.SECOND);
        door0s.add(Door0.THIRD);

        Door0 carDoor0 = chooseRandomDoor();
        Door0 selectedDoor0 = chooseRandomDoor();

        Predicate<Door0> isNotSelectedOrCar = door0 -> door0 != selectedDoor0 && door0 != carDoor0;

        List<Door0> doorsToReveal = door0s.stream()
                .filter(isNotSelectedOrCar)
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

        Door0 revealedDoor0 = doorsToReveal.get(random.nextInt(doorsToReveal.size()));

        Door0 finalChoice = door0s.stream()
                .filter(isNotSelectedOrCar.and(door0 -> door0 != revealedDoor0))
                .findFirst()
                .orElseThrow(IllegalStateException::new);

        return finalChoice == carDoor0;
    }

    private static Door0 chooseRandomDoor() {
        Door0[] door0s = Door0.values();
        return door0s[random.nextInt(door0s.length)];
    }
}
